package com.softsec.isc.analysis;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.softsec.isc.apkextract.FileAction;

public class GetSpecialString {

	 public static Logger logger = LogManager.getLogger("GetSpecialString");
	 
	public static void main(String[] args) throws IOException{
		List<String> stringTest = new ArrayList<String>();
		//stringTest.add("16858518145424727649933056498660090257938028834024614531807279123004174432243450701435915480207331070058018980640996970114664924595721996462420633436577638037200905330595086422869013811312746989152070453400757153846226677525749706019764596866640120258421254940021321226583202460629795560285891968341710281094872056200491420699403567143946437948332885658783820093598141803165201037010008079307300280225532863042313106598043518026300425994352469437334294025333467781054481930686780394079520231376683030299347953241192226954447774155252005439623341985847608294040377722245597091482547502849289313590160972967892168922580");
		stringTest.add("95599");
		String apkname = args[0];
		List<String>fileList = new ArrayList<String>(); 
		FileAction.CalcalateAllFile(apkname, fileList);
		for(int index=1;index<args.length;index++){
			stringTest.add(args[index]);
		}
		logger.warn("STARTING!!!!!!");
		 try {
//			 GetStrings(apkname, stringTest);
			 for (int i = 0; i < fileList.size(); i++) {
				 GetStrings(fileList.get(i), stringTest);
				 List<String> CommonList = DexMapItemMapper.getInstance().getresultMap().get("stringList");
				 if (CommonList != null && !CommonList.isEmpty()) {
					logger.info(fileList.get(i));
					DexMapItemMapper.getInstance().removeResult("stringList");
				}
			 }
			 
		} catch (Exception e) {
			e.printStackTrace();
		}
	 
//		 List<String> CommonList = DexMapItemMapper.getInstance().getresultMap().get("stringList");
//		 List<String> UrlList = DexMapItemMapper.getInstance().getresultMap().get("url");
	//	 List<String> ClassNameList = DexMapItemMapper.getInstance().getresultMap().get("packageName");

//		 for(String index : CommonList){
//			 System.out.println(index);
//		 }
		 logger.warn("FINISHED!!!!!!!");
	}
	
	 public static Map<String,List<String>> GetStrings(String apkname,List<String> stringTest) throws Exception{		
		 try {
			 DexHeader.readDex(apkname);		 
			 GetDexStringId.StringId(apkname);
		//	 GetDexTypeId.TypeId(apkname);
		//	 GetClassName.ClassName(apkname);//获取类名,不需要
			 //TODO 签名
		//	 ApkSignature apkSignature = ApkSignatureExtractor.getApkSignature(apkname);
		 } catch (Exception e) {
			 throw new Exception("error!");			
		 }
		 //比对		
		 GetCommonList(stringTest);
		 DexMapItemMapper.getInstance().getDexMapItemCountMap().clear();
		 DexMapItemMapper.getInstance().getDexMapItemOffsetMap().clear();
		 DexMapItemMapper.getInstance().getDexStringIdMap().clear();
		 DexMapItemMapper.getInstance().getDexStringOffsetMap().clear();
		 System.gc();	 
		 return DexMapItemMapper.getInstance().getresultMap();
	 }
	 
	 @SuppressWarnings({ "rawtypes", "unchecked" })
	public static void GetCommonList(List<String> stringTest){	
		 List<String> CommonList = new ArrayList<String>();
		 List<String> speList = new ArrayList<String>();
	//	 List<String> UrlList = new ArrayList<String>();
		 HashSet hc = new HashSet();
	//	 HashSet hu = new HashSet();
		 HashSet speSet = new HashSet();
		 
		 for(String test : stringTest){
			 for (int index : DexMapItemMapper.getInstance().getDexStringIdMap().keySet()) {
				 String temp = DexMapItemMapper.getInstance().getDexStringIdMap().get(index);
				 if(temp.contains(test)){
					 hc.add(temp);
					 speSet.add(test);
				 }
//				 if(GetRegular(temp)){
//					 hu.add(temp);
//				 }
			 }			 
		 }
		 CommonList.addAll(hc);
		 speList.addAll(speSet);
		 for (int i = 0; i < speList.size(); i++) {
			DexMapItemMapper.getInstance().addToSpecialList(speList.get(i));
		}
	//	 UrlList.addAll(hu);
		 DexMapItemMapper.getInstance().addResult("stringList", CommonList);
	//	 DexMapItemMapper.getInstance().addResult("url", UrlList);
	 }
	 
	 public static boolean GetRegular(String urltest) {
		 boolean b=false;
		 String url="[a-zA-z]{1,5}://[a-zA-Z0-9_]+((\\.|/)[a-zA-Z0-9_]+)*((#|\\?)[\\s\\S]+)?";
		 Pattern pattern=Pattern.compile(url);
		 Matcher matcher = pattern.matcher(urltest);
		 b= matcher.lookingAt();			
		 return b;		
	 }
}
